﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
/// Summary description for PMSBase
/// </summary>
public class PMSBase : SiteAdminBase
{
    private string m_DefaultFormName = "form1";

    public string DefaultFormName
    {
        get { return m_DefaultFormName; }
        set { m_DefaultFormName = value; }
    }

    private string m_DefaultPrex = "P_";

    public string DefaultPrex
    {
        get { return m_DefaultPrex; }
        set { m_DefaultPrex = value; }
    }

    private string m_TableName;

    public string TableName
    {
        get { return m_TableName; }
        set { m_TableName = value; }
    }
    private string m_Key;

    public string Key
    {
        get { return m_Key; }
        set { m_Key = value; }
    }
    protected HtmlForm htmlForm;

    public PMSBase()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    protected string AppendCondition(string[] conditions)
    {
        System.Text.StringBuilder str = new System.Text.StringBuilder();
        foreach (string s in conditions)
        {
            str.Append(",'").Append(s).Append("'");
        }
        str.Remove(0, 1);
        return str.ToString();
    }
    protected void SetControlEmptyValue(System.Web.UI.Control control, string prex)
    {
        int nPageControls = control.Controls.Count;
        string id = string.Empty;
        for (int i = 0; i < nPageControls; i++)
        {
            foreach (System.Web.UI.Control ct in control.Controls[i].Controls)
            {
                if (ct.HasControls())
                {
                    SetControlEmptyValue(ct, prex);
                }
                else
                {
                    id = ct.ID;
                    if (string.IsNullOrEmpty(id) || !id.StartsWith(prex))
                        continue;
                    if (ct is TextBox)
                    {
                        ((TextBox)ct).Text = string.Empty;
                    }
                    else if (ct is HiddenField)
                    {
                        ((HiddenField)ct).Value = string.Empty;
                    }
                    else if (ct is Label)
                    {
                        ((Label)ct).Text = string.Empty;
                    }
                    else if (ct is Openfree.Framework.WebControl.DropDownList)
                    {
                        ((Openfree.Framework.WebControl.DropDownList)ct).SelectByValue = string.Empty;
                    }
                    else if (ct is Openfree.Framework.WebControl.RadioButtonList)
                    {
                        ((Openfree.Framework.WebControl.RadioButtonList)ct).SelectByValue = string.Empty;
                    }
                }
            }
        }
    }
    /// <summary>
    /// 根据前缀，清空Form1中控件的值
    /// </summary>
    protected void ClearPrexControlValues(string prex)
    {
        SetControlEmptyValue(this.Form, prex);
    }
    /// <summary>
    /// 根据前缀，是否设置只读
    /// </summary>
    protected void SetReadOnly(string prex,bool isReadonly)
    {
        SetControlReadOnly(this.Form, prex,isReadonly);
    }

    protected void SetControlReadOnly(System.Web.UI.Control control, string prex,bool isReadonly)
    {
        int nPageControls = control.Controls.Count;
        string id = string.Empty;
        for (int i = 0; i < nPageControls; i++)
        {
            foreach (System.Web.UI.Control ct in control.Controls[i].Controls)
            {
                if (ct.HasControls())
                {
                    SetControlReadOnly(ct, prex, isReadonly);
                }
                else
                {
                    id = ct.ID;
                    if (string.IsNullOrEmpty(id) || !id.StartsWith(prex))
                        continue;
                    if (ct is TextBox)
                    {
                        ((TextBox)ct).ReadOnly  = isReadonly;
                    } 
                    else if (ct is Openfree.Framework.WebControl.DropDownList)
                    {
                        ((Openfree.Framework.WebControl.DropDownList)ct).Attributes.Add("readonly", "" + isReadonly);
                    }
                    else if (ct is Openfree.Framework.WebControl.RadioButtonList)
                    {
                        ((Openfree.Framework.WebControl.RadioButtonList)ct).Attributes.Add("readonly", "" + isReadonly);
                    }
                }
            }
        }
    }
    /// <summary>
    ///根据前缀填充DataRow中的值，清空Form1中控件的值
    /// </summary>
    /// <param name="dr">DataRow</param>
    /// <param name="prex">前缀</param>
    protected void FillDataRowByPrex(ref DataRow dr, string prex, EnumOperator kind)
    {
        string[] keys = Request.Form.AllKeys;
        string fname = "";
        string value = string.Empty;
        string[] s = null;
        foreach (string str in keys)
        {
            s = str.Split("$".ToCharArray());
            fname = s[s.Length - 1];
            if (fname.StartsWith(prex))
            {
                fname = fname.Replace(prex, string.Empty);
                value = Request.Form.GetValues(str)[0];
                if (kind == EnumOperator.ADD && !string.IsNullOrEmpty(value))
                {
                    dr[fname] = value;
                }
                else if (kind == EnumOperator.EDIT)
                {
                    if (string.IsNullOrEmpty(value))
                    {
                        dr[fname] = DBNull.Value;
                    }
                    else
                    {
                        dr[fname] = value;
                    }
                }
            }
        }
        PMSAccess.AppendValue(ref dr, kind);
    }
    /// <summary>
    /// 根据前缀填充Form1中控件的值
    /// </summary>
    /// <param name="dr">DataRow</param>
    /// <param name="prex">前缀</param>
    protected void FillValueByPrexFromDataRow(DataRow dr, string prex)
    {
        this.SetControlValue(this.Form, prex, dr);
    }
    protected void SetControlValue(System.Web.UI.Control control, string prex, DataRow dr)
    {
        int nPageControls = control.Controls.Count;
        string id = string.Empty;
        for (int i = 0; i < nPageControls; i++)
        {
            foreach (System.Web.UI.Control ct in control.Controls[i].Controls)
            {
                if (ct.HasControls())
                {
                    SetControlValue(ct, prex, dr);
                }
                else
                {
                    id = ct.ID;
                    if (string.IsNullOrEmpty(id) || !id.StartsWith(prex))
                        continue;
                    id = id.Replace(prex, string.Empty);
                    if (ct is TextBox)
                    {
                        if (dr[id] is DateTime)
                        {
                            ((TextBox)ct).Text = ((DateTime)dr[id]).ToString("yyyy-MM-dd");
                        }
                        else
                        {
                            ((TextBox)ct).Text = dr[id].ToString();
                        }
                    }
                    else if (ct is HiddenField)
                    {
                        ((HiddenField)ct).Value = dr[id].ToString();
                    }
                    else if (ct is Label)
                    {
                        ((Label)ct).Text = dr[id].ToString();
                    }
                    else if (ct is Openfree.Framework.WebControl.DropDownList)
                    {
                        ((Openfree.Framework.WebControl.DropDownList)ct).SelectByValue = dr[id].ToString();
                    }
                    else if (ct is Openfree.Framework.WebControl.RadioButtonList)
                    {
                        ((Openfree.Framework.WebControl.RadioButtonList)ct).SelectByValue = dr[id].ToString();
                    }
                }
            }
        }
    }
    protected void BindDropDownList(Openfree.Framework.WebControl.DropDownList control, DataSet ds, bool AddEmpty)
    {
        if (ds != null && ds.Tables.Count > 0)
        {
            control.DataSource = ds.Tables[0];
            control.DataTextField = "CodeName";
            control.DataValueField = "CodeValue";
            control.DataBind();
        }
        if (AddEmpty)
        {
            control.Items.Insert(0, new ListItem("请选择", ""));
        }
        if (control.Items.Count > 0)
        {
            control.SelectByValue = string.Empty;
        }
    }
    public string ParseDateToShort(string s)
    {
        if(string.IsNullOrEmpty(s)) return string.Empty;
        DateTime dt= DateTime.Now;
        if (DateTime.TryParse(s, out dt))
        {
            return dt.ToString("yyyy-MM-dd");
        }
        return string.Empty;
    }
    protected void DownLoadFile(string filePath,string fileName)
    {

        if (System.IO.File.Exists(filePath))
        {
            //开始下载 
            //以字符流的形式下载文件
            System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open);
            byte[] bytes = new byte[(int)fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            Response.ContentType = "application/octet-stream";
            //通知浏览器下载文件而不是打开
            Response.AddHeader("Content-Disposition", "attachment;  filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }
    }
}


